/**
 * related to detail.ui
 * 
 * @Author : chenglm
 * @Timestamp : 2016-12-19
 */
var page = sm("do_Page");
var app = sm("do_App");
var nf = sm("do_Notification");
var global = deviceone.sm("do_Global");
var helperOpen = require("helper/open");
var qqHttp = require("helper/http");
var mallUrl = require("url/mall");
var imgTool = require("helper/picture");
var hashdata = mm("do_HashData");
var linearLayout_main = ui("do_LinearLayout_main");
var shopId = null;
var resume = '',busStart='',busEnd='';
var payType = '',sendDeliveryType='',status='';
var style = '';
var quanPayRate = '';
var communityId = null;
var extCommunityIds = '';//服务社区ids
var extCommunityNames = '';

var x,y=null;//店铺坐标

/* 头部 */
var headerAout = ui("do_ALayout_header");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
var header = ui("header");
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
var labelTitle = ui(addheader+".labelTitle");
labelTitle.text='我的店铺';
headerLeft.on("touch", function() {
	app.closePage();
});

//andriod返回键
page.on("back", function() {
    app.closePage();
});
page.on("loaded", function(){
	init();
});
app.on("fireShopUpdate",function(d){
	if(d.logo&&d.logo!==""){
		ui(addShopLogo+".thumb").source = d.logo;
	}
    if(d.name && d.name!==""){
    	ui(addShopName+".labelText").text = d.name;
	}
    if(d.phone && d.phone!==""){
    	ui(addShopPhone+".labelText").text = d.phone;
	}
    if(d.address && d.address!==""){
    	ui(addShopAddress+".labelText").text = d.address;
    	x = d.dingweiX;
    	y = d.dingweiY;
	}
    /*if(d.payTypeStr && d.payTypeStr!==""){
    	ui(addPayType+".labelText").text = d.payTypeStr;
    	payType = d.payType;
	}*/
    if(d.sendDeliveryTypeStr && d.sendDeliveryTypeStr!==""){
    	ui(addSendDeliveryType+".labelText").text = d.sendDeliveryTypeStr;
    	sendDeliveryType = d.sendDeliveryType;
	}
    if(d.deliveryValue != null && d.deliveryValue !== ""){
    	ui(addDeliveryValue+".labelText").text = d.deliveryValue;
	}
    if(d.freight != null && d.freight !== ""){
    	ui(addFreight+".labelText").text = d.freight;
	}
    if(d.resume && d.resume!==""){
    	resume = d.resume;
	}
    if(d.licensePic&&d.licensePic!==""){
		ui(addShopLicensePic+".thumb").source = d.licensePic;
	}
    if(d.busStart != null && d.busEnd != null){
    	busStart = d.busStart;
    	busEnd = d.busEnd;
    	ui(addShopBusTime+".labelText").text = busStart+'~'+busEnd;
	}
    if(d.status != null){
    	status = d.status;
    	if (d.status==1) {
    		ui(addShopStatus+".labelText").text = '正常';
    	} else {
    		ui(addShopStatus+".labelText").text = '暂停';
    	}
	}
    if(d.quanPayRate != null && d.quanPayRate !== ""){
    	quanPayRate = d.quanPayRate;
    	ui(addQuanPayRate+".labelText").text = d.quanPayRate*100+"%";
	}
    //服务社区
    if(d.extCommunityIds!=null && d.extCommunityIds!=""){
    	extCommunityIds = d.extCommunityIds;
    	extCommunityNames = d.extCommunityNames;
    	ui(addExtCommunity+".labelText").text = d.extCommunityNames;
    	//nf.toast(extCommunityIds+"  "+extCommunityNames);
    	//更新店铺--服务社区数据库
    	updateExtCommunity();
    	
    }
});

function updateExtCommunity(){
	if(extCommunityIds!=null && extCommunityIds!=""){
		var paramData = {
				id:shopId,
				extCommunityIds:extCommunityIds,
				extCommunityNames:extCommunityNames
			};
		qqHttp.post(mallUrl.url.shop.updateExtCommunity,paramData,function(data){
			nf.toast("修改服务社区成功！");
		});
	}
}
/*logo*/
var addShopLogo = linearLayout_main.add("shopLogo", "source://view/user/base/userLogoLabel.ui",0,0);
var shopLogo = ui("shopLogo");
shopLogo.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "店铺logo",
    "labelTipsVisible":false
});
shopLogo.refreshData();
//更换头像
shopLogo.on("touch", function() {
	imgTool.imgCut(selectImgBack);//选择图片并裁剪
});
function selectImgBack(imgList){
	qqHttp.form(imgList,"mall/shop",function(imgDatas){
		var string4imgs = imgDatas.path;//返回图片云端路径
		var logoData = {
			id:shopId,
			logo:string4imgs
		}
		qqHttp.post(mallUrl.url.shop.updateLogo,logoData,function(data){
			nf.toast("logo上传成功");
			app.fire("fireShopUpdate",{"logo":data.mallShop.fullLogo});
			app.fire("fireShopFromUpdate",{"logo":data.mallShop.fullLogo});
		});
	});
};
/*店铺名称*/
var addShopName = linearLayout_main.add("shopName", "source://view/reuse/form/textLabel.ui","shopLogo");
var shopName = ui("shopName");
shopName.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "店铺名称",
    "labelText":'',
    "labelTipsVisible":false,
    "labelTips":""
});
shopName.refreshData();
//修改店铺名称
shopName.on("touch", function() {
	helperOpen.open("source://view/qshop/manage/shop/editName.ui", {
		id : shopId,
		name : ui(addShopName+".labelText").text
	});
});
/*店铺类型*/
var addShopStyle = linearLayout_main.add("shopStyle", "source://view/reuse/form/textLabel.ui","shopName");
var shopStyle = ui("shopStyle");
shopStyle.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "店铺类型",
    "labelText":'',
    "labelTipsVisible":false,
    "labelTips":""
});
shopStyle.refreshData();
/*店铺简介*/
var addShopResume = linearLayout_main.add("shopResume", "source://view/reuse/form/textLabel.ui","shopStyle");
var shopResume = ui("shopResume");
shopResume.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "店铺简介",
    "labelText":'',
    "labelTipsVisible":true,
    "labelTips":"修改店铺简介"
});
shopResume.refreshData();
//修改店铺简介
shopResume.on("touch", function() {
	helperOpen.open("source://view/qshop/manage/shop/editResume.ui", {
		id : shopId,
		resume : resume
	});
});

var addLine1 = linearLayout_main.add("line1", "source://view/reuse/form/line.ui","shopResume");
var line1 = ui("line1");
line1.refreshData();
/*营业状态*/
var addShopStatus = linearLayout_main.add("shopStatus", "source://view/reuse/form/textLabel.ui","line1");
var shopStatus = ui("shopStatus");
shopStatus.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "营业状态",
    "labelText":'',
    "labelTipsVisible":false,
    "labelTips":""
});
shopStatus.refreshData();
//修改营业状态
shopStatus.on("touch", function() {
	helperOpen.open("source://view/qshop/manage/shop/editStatus.ui", {
		id : shopId,
		status : status
	});
});
/*主营类目*/
var addShopCategory = linearLayout_main.add("shopCategory", "source://view/reuse/form/textLabel.ui","shopStatus");
var shopCategory = ui("shopCategory");
shopCategory.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "主营类目",
    "labelText":'',
    "labelTipsVisible":false,
    "labelTips":""
});
shopCategory.refreshData();
//修改主营类目
shopCategory.on("touch", function() {
	global.setMemory("shopCategoryCount", 0);
	helperOpen.open("source://view/qshop/manage/shop/selectCategory.ui",{parentId:0,categoryStr:'',type:style});
});
/*店铺营业时间*/
var addShopBusTime = linearLayout_main.add("shopBusTime", "source://view/reuse/form/textLabel.ui","shopCategory");
var shopBusTime = ui("shopBusTime");
shopBusTime.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "营业时间",
    "labelText":'',
    "labelTipsVisible":false,
    "labelTips":""
});
shopBusTime.refreshData();
//修改营业时间
shopBusTime.on("touch", function() {
	helperOpen.open("source://view/qshop/manage/shop/editBusTime.ui", {
		id : shopId,
		busStart : busStart,
		busEnd : busEnd
	});
});
/*店铺服务社区*/
var addExtCommunity = linearLayout_main.add("extCommunity", "source://view/reuse/form/textLabel.ui","shopBusTime");
var extCommunity = ui("extCommunity");
extCommunity.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "服务社区",
    "labelText":'',
    "labelTipsVisible":false,
    "labelTips":""
});
extCommunity.refreshData();
//修改服务社区
extCommunity.on("touch", function() {
	helperOpen.open("source://view/community/distance/select.ui", {
		id : shopId,
		extCommunityIds : extCommunityIds,
		x: x,
		y: y
	});
});

/*店铺联系*/
var addShopPhone = linearLayout_main.add("shopPhone", "source://view/reuse/form/textLabel.ui","extCommunity");
var shopPhone = ui("shopPhone");
shopPhone.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "联系方式",
    "labelText":'',
    "labelTipsVisible":false,
    "labelTips":""
});
shopPhone.refreshData();
//修改店铺联系
shopPhone.on("touch", function() {
	helperOpen.open("source://view/qshop/manage/shop/editPhone.ui", {
		id : shopId,
		phone : ui(addShopPhone+".labelText").text
	});
});
/*店铺地址*/
var addShopAddress = linearLayout_main.add("shopAddress", "source://view/reuse/form/textLabel.ui","shopPhone");
var shopAddress = ui("shopAddress");
shopAddress.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "联系地址",
    "labelText":'',
    "labelTipsVisible":false,
    "labelTips":""
});
shopAddress.refreshData();
//修改店铺地址
shopAddress.on("touch", function() {
	helperOpen.open("source://view/qshop/manage/shop/editAddressV1.ui", {
		id : shopId,
		address : ui(addShopAddress+".labelText").text
	});
});
/*支付方式*/
/*var addPayType = linearLayout_main.add("payType", "source://view/reuse/form/textLabel.ui","shopAddress");
var payType = ui("payType");
payType.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "支付方式",
    "labelText":'',
    "labelTipsVisible":false,
    "labelTips":""
});
payType.refreshData();

payType.on("touch", function() {
	helperOpen.open("source://view/qshop/manage/shop/editPayType.ui", {
		id : shopId,
		payType : payType
	});
});*/
/*配送类型*/
var addSendDeliveryType = linearLayout_main.add("sendDeliveryType", "source://view/reuse/form/textLabel.ui","shopAddress");
var sendDeliveryType = ui("sendDeliveryType");
sendDeliveryType.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "配送类型",
    "labelText":'',
    "labelTipsVisible":false,
    "labelTips":""
});
sendDeliveryType.refreshData();
//修改店铺配送类型
sendDeliveryType.on("touch", function() {
	helperOpen.open("source://view/qshop/manage/shop/editSendDeliveryType.ui", {
		id : shopId,
		shopStyle: style,
		sendDeliveryType : sendDeliveryType
	});
});

/*起送金额*/
var addDeliveryValue = linearLayout_main.add("deliveryValue", "source://view/reuse/form/textLabel.ui","sendDeliveryType");
var deliveryValue = ui("deliveryValue");
deliveryValue.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "起送金额",
    "labelText":'',
    "labelTipsVisible":false,
    "labelTips":""
});
deliveryValue.refreshData();
//修改起送金额
deliveryValue.on("touch", function() {
	helperOpen.open("source://view/qshop/manage/shop/editDeliveryValue.ui", {
		id : shopId,
		deliveryValue : ui(addDeliveryValue+".labelText").text
	});
});

/*配送费*/
var addFreight = linearLayout_main.add("freight", "source://view/reuse/form/textLabel.ui","deliveryValue");
var freight = ui("freight");
freight.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "配送费",
    "labelText":'',
    "labelTipsVisible":false,
    "labelTips":""
});
freight.refreshData();
//修改配送费
freight.on("touch", "", 300, function() {
	helperOpen.open("source://view/qshop/manage/shop/editFreight.ui", {
		id : shopId,
		freight : ui(addFreight+".labelText").text
	});
});

/*圈币结算比例*/
var addQuanPayRate = linearLayout_main.add("quanPayRate", "source://view/reuse/form/textLabel.ui","freight");
var quanPayRate = ui("quanPayRate");
quanPayRate.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "圈币结算比例",
    "labelText":'',
    "labelTipsVisible":false,
    "labelTips":""
});
quanPayRate.refreshData();
//圈币结算比例
quanPayRate.on("touch", "", 300, function() {
	helperOpen.open("source://view/qshop/manage/shop/editQuanPayRate.ui", {
		id : shopId,
		quanPayRate : quanPayRate
	});
});

/*审核状态*/
var addAuditStatus = linearLayout_main.add("auditStatus", "source://view/reuse/form/textLabel.ui","quanPayRate");
var auditStatus = ui("auditStatus");
auditStatus.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "审核状态",
    "labelText":'',
    "labelTipsVisible":false,
    "labelTips":""
});
auditStatus.refreshData();
/*营业执照*/
var addShopLicensePic = linearLayout_main.add("shopLicensePic", "source://view/user/base/userLogoLabel.ui","auditStatus");
var shopLicensePic = ui("shopLicensePic");
shopLicensePic.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "营业执照",
    "labelTipsVisible":false
});
shopLicensePic.refreshData();
//更换头像
shopLicensePic.on("touch", function() {
	imgTool.imgCut(selectImgBack2);//选择图片并裁剪
});
function selectImgBack2(imgList){
	qqHttp.form(imgList,"mall/shop",function(imgDatas){
		var string4imgs = imgDatas.path;//返回图片云端路径
		var logoData = {
			id:shopId,
			licensePic:string4imgs
		}
		qqHttp.post(mallUrl.url.shop.updateLicensePic,logoData,function(data){
			nf.toast("营业执照上传成功");
			app.fire("fireShopUpdate",{"licensePic":data.mallShop.fullLicensePic});
		});
	});
};
function init(){
	qqHttp.post(mallUrl.url.shop.detail, {}, function(data) {
		var mallShop = data.mallShop;
		shopId = mallShop.id;
		resume = mallShop.resume;
		//payType = mallShop.payType;
		busStart = mallShop.busStart;
		busEnd = mallShop.busEnd;
		sendDeliveryType = mallShop.sendDeliveryType;
		style = mallShop.style;
		status = mallShop.status;
		communityId = mallShop.communityId;
		extCommunityIds = mallShop.extCommunityIds;
		extCommunityNames = mallShop.extCommunityNames;
		x = mallShop.x;
		y = mallShop.y;
		ui(addShopName+".labelText").text = mallShop.name;
		ui(addShopStyle+".labelText").text = mallShop.strStyle;
		ui(addShopCategory+".labelText").text = mallShop.categoryName;
		ui(addShopPhone+".labelText").text = mallShop.phone;
		ui(addShopAddress+".labelText").text = mallShop.address;
		//ui(addPayType+".labelText").text = mallShop.payTypeStr;
		ui(addSendDeliveryType+".labelText").text = mallShop.sendDeliveryTypeStr;
		ui(addDeliveryValue+".labelText").text = mallShop.deliveryValue;
		ui(addFreight+".labelText").text = mallShop.freight;
		ui(addShopBusTime+".labelText").text = busStart+'~'+busEnd;
		
		ui(addExtCommunity+".labelText").text = extCommunityNames;
		
		if(!mallShop.logo){
			ui(addShopLogo+".thumb").source = "source://image/common/userLogin.png";
		}else{
			ui(addShopLogo+".thumb").source = mallShop.fullLogo;
		}
		if(mallShop.style == 1){
			ui(addShopLicensePic).visible = false;
		}else{
			if(mallShop.licensePic){
				ui(addShopLicensePic+".thumb").source = mallShop.fullLicensePic;
			}
		}
		if (mallShop.auditStatus == 1) {
			ui(addAuditStatus+".labelText").text = "待审核";
		} else if (mallShop.auditStatus == 2) {
			ui(addAuditStatus+".labelText").text = "已通过";
			ui(addShopLicensePic).visible = false;
		} else {
			ui(addAuditStatus+".labelText").text = "不通过";
		}
		if (mallShop.status == 1) {
			ui(addShopStatus+".labelText").text = '正常';
		} else {
			ui(addShopStatus+".labelText").text = '暂停';
		}
		quanPayRate = mallShop.quanPayRate;
		ui(addQuanPayRate+".labelText").text = mallShop.quanPayRate*100+"%";
		
		if(mallShop.isUseQuanPay == '2'){
			//如果未启用圈币结算，不显示
			ui(addQuanPayRate).visible = false;
			linearLayout_main.redraw();
		}
	});
};
page.on("result", function(data) {
	if (data.fromPage && data.fromPage == 'selectShopCategory') {
		var paramData = {
			id:shopId,
			categoryId:data.parentId
		};
		qqHttp.post(mallUrl.url.shop.updateCategoryId, paramData, function(data) {
			nf.toast("修改了主营类目");
			ui(addShopCategory+".labelText").text = data.mallShop.categoryName;
		});
	}
});